<?php


namespace logicmodel;


use comservice\GetRedis;
use comservice\Response;
use datamodel\Author;
use datamodel\Goods;
use datamodel\GoodsCategory;
use datamodel\GoodsOrders;
use datamodel\GoodsTransfer;
use datamodel\OrdersGoods;
use datamodel\Users;
use datamodel\UsersGoods;
use logicmodel\award\Recommend;
use think\Db;
use think\Exception;

class GoodsLogic
{
    private $usersGoodsData;
    private $redis;
    public function __construct()
    {
        $this->usersGoodsData = new UsersGoods();
        $this->redis = GetRedis::getRedis();
    }

    /**
     * 搜索
     * @param $search
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search($search){
        $goods =  (new Goods())->alias('g')
            ->join('goods_category gc','gc.id = g.goods_category_id')
            ->where(['g.is_show'=>1,'g.is_del'=>0,'g.name'=>['like','%'.$search.'%'],'gc.is_convert'=>0,'gc.is_show'=>1,'gc.is_del'=>0])
            ->field(['g.id','g.name','g.image'])
            ->order(['g.order asc'])
            ->select();
        $author =  (new Author())->where(['is_show'=>1,'is_del'=>0,'name'=>['like','%'.$search.'%']])->select();
        $goods = !empty($goods)?collection($goods):[];
        $author = !empty($author)?collection($author):[];
        return Response::success('success',['goods'=>$goods,'author'=>$author]);
    }
    /**
     * 作品分类
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goodsCategoryList(){
        $data =  (new GoodsCategory())->where(['is_show'=>1,'is_del'=>0,'is_convert'=>0])->order(['order asc'])->select();
        if($data){
            $data = collection($data)->toArray();
            return Response::success('success',$data);
        }
        return Response::success('success',[]);
    }
    /**
     * 作品分类
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goodsCategory(){
        $data =  (new GoodsCategory())->where(['is_show'=>1,'is_del'=>0,'is_convert'=>0])->order(['order asc'])->limit(3)->select();
        if($data){
            $data = collection($data)->toArray();
            $goodsData = new Goods();
            $field = ['g.*','a.name author_name','a.image author_image','n.name network_name'];
            $order = ['g.order asc'];
            foreach ($data as $key=>&$value){
                $goods_category_id = $value['id'];
                $goods =  $goodsData->alias('g')
                    ->join('author a','a.id = g.author_id')
                    ->join('network n','n.id = g.network_id')
                    ->join('goods_category gc','gc.id = g.goods_category_id')
                    ->where(['g.is_show'=>1,'g.is_del'=>0,'g.goods_category_id'=>$goods_category_id])
                    ->order($order)
                    ->field($field)
                    ->limit(2)
                    ->select();
                if(!empty($goods)){
                    $goods = collection($goods)->toArray();
                    $time = date('Y-m-d H:i:s');
                    foreach ($goods as $k=>$v){
                        if( $time >= $v['start_time']){
                            if($v['surplus'] > 0){
                                $goods[$k]['status'] = 1; //售卖中
                                $goods[$k]['status_text'] = '抢购中';
                            }else{
                                $goods[$k]['status'] = 2; //已售罄
                                $goods[$k]['status_text'] = '已售罄';
                            }
                        }else{
                           $diff_time =  strtotime($v['start_time']) - strtotime($time); //时间差
                            if($diff_time >= 86400) {
                                //未开售
                                $goods[$k]['status'] = 3; //显示开售时间
                                $goods[$k]['status_text'] = '敬请期待 '.date('m.d H:i').' 开售';
                            }else{
                                //未开售
                                $goods[$k]['status'] = 4; //显示倒计时
                                $goods[$k]['status_text'] = '倒计时';
                                $goods[$k]['diff_time'] = $diff_time;
                            }
        
                        }
                    }
                   
                    $goods = addWebSiteUrl($goods,['image','author_image']);

                }else{
                    $goods =[];
                }
 
                $data[$key]['goods'] = $goods;
            }
            return Response::success('success',$data);
        }
        return Response::success('success',[]);
    }
    /**
     * 新品商品列表
     * @param $goods_id
     * @param $author_id
     * @param $time_order
     * @param $price_order
     * @param $is_home
     * @param $goods_category_id
     * @param $page
     * @param $pagesize
     * @return array
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goodsList($goods_id,$author_id,$time_order,$price_order,$goods_category_id,$page,$pagesize){
        $where['g.is_del'] = 0;
        $where['g.is_show'] = 1;
        $where['gc.is_convert'] = 0;
        $where['gc.is_del'] = 0;
        $where['gc.is_show'] = 1;
        if(!empty($goods_category_id))$where['g.goods_category_id'] = $goods_category_id;
        if(!empty($goods_id)) $where['g.id'] = $goods_id;
        if(!empty($author_id)) $where['a.id'] = $author_id;

        if($time_order > 0){
            if($time_order == 1){
                $order = ['g.start_time asc','g.order asc'];
            }else{
                $order = ['g.start_time desc','g.order asc'];
            }

        }elseif ($price_order > 0){
            if($price_order == 1){
                $order = ['g.price asc','g.order asc'];
            }else{
                $order = ['g.price desc','g.order asc'];
            }
        }else{
            $order = ['g.order asc'];
        }
        $goodsData =  new Goods();
        $count = $goodsData->alias('g')
            ->join('author a','a.id = g.author_id')
            ->join('network n','n.id = g.network_id')
            ->join('goods_category gc','gc.id = g.goods_category_id')
            ->where($where)
            ->count();
        if($count <= 0) return Response::success('暂无数据',['count'=>$count,'data'=>[],'page'=>$page,'pagesize'=>$pagesize]);
        $data = $goodsData
            ->alias('g')
            ->join('author a','a.id = g.author_id')
            ->join('network n','n.id = g.network_id')
            ->join('goods_category gc','gc.id = g.goods_category_id')
            ->where($where)
            ->order($order)
            ->field(['g.*','a.name author_name','a.image author_image','n.name network_name'])
            ->page($page,$pagesize)
            ->select();
        if(!empty($data)){
            $data = collection($data)->toArray();

            $time = date('Y-m-d H:i:s');
            foreach ($data as $k=>$v){
                if( $time >= $v['start_time']){
                    if($v['surplus'] > 0){
                        $data[$k]['status'] = 1; //售卖中
                        $data[$k]['status_text'] = '抢购中';
                    }else{
                        $data[$k]['status'] = 2; //已售罄
                        $data[$k]['status_text'] = '已售罄';
                    }
                }else{
                   $diff_time =  strtotime($v['start_time']) - strtotime($time); //时间差
                    if($diff_time >= 86400) {
                        //未开售
                        $data[$k]['status'] = 3; //显示开售时间
                        $data[$k]['status_text'] = '敬请期待 '.date('m.d H:i').' 开售';
                    }else{
                        //未开售
                        $data[$k]['status'] = 4; //显示倒计时
                        $data[$k]['status_text'] = '倒计时';
                        $data[$k]['diff_time'] = $diff_time;
                    }

                }
            }

            $data = addWebSiteUrl($data,['image','author_image']);
        }
        return Response::success('success',['count'=>$count,'data'=>$data,'page'=>$page,'pagesize'=>$pagesize]);
    }

    /**
     * 积分NFT兑换
     * @param $page
     * @param $pagesize
     * @return array
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function integralGoodsList($page,$pagesize){
        $where['g.is_del'] = 0;
        $where['g.is_show'] = 1;
        $where['gc.is_convert'] = 1;
        $where['gc.is_del'] = 0;
        $where['gc.is_show'] = 1;
        $order = ['g.order asc'];
        $goodsData =  new Goods();
        $count = $goodsData->alias('g')
            ->join('author a','a.id = g.author_id')
            ->join('network n','n.id = g.network_id')
            ->join('goods_category gc','gc.id = g.goods_category_id')
            ->where($where)
            ->count();
        if($count <= 0) return Response::success('暂无数据',['count'=>$count,'data'=>[],'page'=>$page,'pagesize'=>$pagesize]);
        $data = $goodsData
            ->alias('g')
            ->join('author a','a.id = g.author_id')
            ->join('network n','n.id = g.network_id')
            ->join('goods_category gc','gc.id = g.goods_category_id')
            ->where($where)
            ->order($order)
            ->field(['g.*','a.name author_name','a.image author_image','n.name network_name'])
            ->page($page,$pagesize)
            ->select();
        if(!empty($data)){
            $data = collection($data)->toArray();
            $data = addWebSiteUrl($data,['image','author_image']);
        }
        return Response::success('success',['count'=>$count,'data'=>$data,'page'=>$page,'pagesize'=>$pagesize]);
    }


    /**
     * 商品详情
     * @param $id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function  goodsDetail($id){
        $where['g.is_del'] = 0;
        $where['g.is_show'] = 1;
        $where['g.id'] = $id;
        $field = ['g.*','gc.name goods_category_name','a.name author_name','a.image author_image','n.name network_name','gc.is_convert'];
        $goodsData =  new Goods();
        $data = $goodsData
            ->alias('g')
            ->join('author a','a.id = g.author_id')
            ->join('goods_category gc','gc.id = g.goods_category_id')
            ->join('network n','n.id = g.network_id')
            ->where($where)
            ->field($field)
            ->find();
        if($data) {
            $data = $data->toArray();
            $time = date('Y-m-d H:i:s');
            if( $time >= $data['start_time']){
                if($data['surplus'] > 0){
                    $data['status'] = 1; //售卖中
                    $data['status_text'] = '抢购中';
                }else{
                    $data['status'] = 2; //已售罄
                    $data['status_text'] = '已售罄';
                }
            }else{
                $diff_time =  strtotime($data['start_time']) - strtotime($time); //时间差
                if($diff_time >= 86400) {
                    //未开售
                    $data['status'] = 3; //显示开售时间
                    $data['status_text'] = '敬请期待 '.date('m.d H:i').' 开售';
                }else{
                    //未开售
                    $data['status'] = 4; //显示倒计时
                    $data['status_text'] = '倒计时';
                    $data['diff_time'] = $diff_time;
                }
            }
            $data = addWebSiteUrl($data,['author_image','image','images']);
            $data['content'] = content($data['content']);
            return Response::success('success',$data);
        }
        return Response::fail('商品信息错误');
    }

    /**
     * 交易记录
     * @param $goods_id
     * @param $page
     * @param $pagesize
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ordersRecord($goods_id,$page,$pagesize){
        $where['o.goods_id'] = $goods_id;
        $where['o.status'] = 1;
        $goodsOrdersData= new GoodsOrders();
        $count = $goodsOrdersData->alias('o')
            ->join('users u','u.id = o.sale_uid')
            ->where($where)->count();
        if($count <= 0) return Response::success('暂无数据',['count'=>$count,'data'=>[],'page'=>$page,'pagesize'=>$pagesize]);
        $data =  $goodsOrdersData->alias('o')
            ->join('users u','u.id = o.sale_uid')
            ->where($where)->order(['o.id desc'])
            ->field(['u.head_image','u.nick_name','o.price','o.create_time'])
            ->page($page,$pagesize)
            ->select();
        return Response::success('暂无数据',['count'=>$count,'data'=>$data,'page'=>$page,'pagesize'=>$pagesize]);
    }

    /**商品列表
     * @param $goods_id
     * @param $page
     * @param $pagesize
     * @param $users_goods_id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function tradeRecord($goods_id,$users_goods_id,$page,$pagesize){
        $where['ug.goods_id'] = $goods_id;
        $where['ug.status'] = ['in',[2]];
        if(!empty($users_goods_id)) $where['ug.id'] = ['<>',$users_goods_id];
        $usersGoodsData = new UsersGoods();
        $count =  $usersGoodsData->alias('ug')
            ->join('users u','u.id = ug.uid')
            ->join('goods g','g.id = ug.goods_id')
            ->where($where)
            ->count();
        if($count <= 0) return Response::success('暂无数据',['count'=>$count,'data'=>[],'page'=>$page,'pagesize'=>$pagesize]);
        $field = ['ug.*','g.name goods_name','u.nick_name','u.head_image','g.stock'];
        $data = $usersGoodsData->alias('ug')
            ->join('users u','u.id = ug.uid')
            ->join('goods g','g.id = ug.goods_id')
            ->field($field)
            ->where($where)
            ->order(['ug.order asc','ug.sales_time asc'])
            ->page($page,$pagesize)
            ->select();
        return Response::success('暂无数据',['count'=>$count,'data'=>$data,'page'=>$page,'pagesize'=>$pagesize]);
    }
    /**
     * NFT购买
     * @param $userInfo
     * @param $id
     * @param $pay_type
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function apply($userInfo,$id,$pay_type){
        if($userInfo['is_auth'] == 0) return Response::bindAuth();

        if( $this->redis->getItem('goods_order_'.$userInfo['id'])){
            return Response::fail('频繁操作');
        }
        $this->redis->setItem('goods_order_'.$userInfo['id'],0);
        $this->redis->settime('goods_order_'.$userInfo['id'],3);
        //虚拟用户购买
        $goodsData = new Goods();
        $goodsInfo = $goodsData->alias('g')
            ->join('goods_category gc','gc.id= g.goods_category_id')
            ->where(['g.id'=>$id,'g.is_show'=>1,'g.is_del'=>0])
            ->field(['g.*','gc.is_convert'])
            ->find();
        if(empty($goodsInfo)) return Response::fail('作品信息错误');
        if($goodsInfo['surplus'] <= 0 ) return Response::fail('已售罄');
        $is_convert = $goodsInfo['is_convert'];
        if($is_convert  == 0){
            $time = date('Y-m-d H:i:s');
            if($time < $goodsInfo['start_time']) return Response::fail('未到开售时间');
            return $this->newGoods($userInfo,$goodsInfo,$pay_type);
        }else{
            return $this->integralGoods($userInfo['id'],$goodsInfo);
        }
    }

    /**
     * 新品下单
     * @param $userInfo
     * @param $goodsInfo
     * @param $pay_type
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function newGoods($userInfo,$goodsInfo,$pay_type){
        $uid = $userInfo['id'];
        $price  = $goodsInfo['price'];
        $goods_id = $goodsInfo['id'];
        $order_num = uniqueNum();
        $time = date('Y-m-d H:i:s');
        if($pay_type == 1){
            //余额抵扣
            Db::startTrans();
            $result = (new AccountLogic())->subAccount($uid,1,$price,'购买新品','购买新品'.$goodsInfo['name'].'账户扣除');
            if(!$result){
                Db::rollback();
                return Response::fail('账户余额不足');
            }
            //生成购买订单
            $order['uid'] = $uid;
            $order['goods_id'] = $goods_id;
            $order['order_num'] = $order_num;
            $order['goods_category_id'] = 1;
            $order['number'] = 1;
            $order['price'] = $price;
            $order['total_price'] = $price;
            $order['pay_type'] = $pay_type;
            $order['pay_time'] = $time;
            $order['create_time'] = $time;
            $order['status'] = 1;
            $result = (new OrdersGoods())->insertGetId($order);
            if(!$result){
                Db::rollback();
                return Response::fail('购买失败');
            }

            $goods['uid'] = $uid;
            $goods['goods_id'] = $goods_id;
            $goods['price'] = $price;
            $goods['create_time'] = date('Y-m-d H:i:s');
            $result =  $this->usersGoodsData->saveEntityAndGetId($goods);
            if($result >0){
                Db::commit();
                $goodsData = new Goods();
                $goodsData->where(['id'=>$goods_id])->setInc('sales');
                $goodsData->where(['id'=>$goods_id])->setDec('surplus');
                (new Users())->where(['id'=>$uid])->setInc('buy_goods_number',1);
                if($userInfo['buy_goods_number'] == 0) (new Recommend())->award($userInfo['pid'],$uid,'推荐会员'.$userInfo['nick_name'].'完成交易奖励',2);
                return Response::success('购买成功',['is_pay'=>1]);
            }
            Db::rollback();
            return Response::fail('购买失败');
        }elseif($pay_type == 2){
            //微信支付
            if($userInfo['is_wx_auth'] == 0 || empty($userInfo['wx_open_id'])) return Response::wxAuth();
            //生成支付信息
            $notify_url = config('site.wx_orders_goods_notify');
            $pay =  (new WxLogic())->pay($order_num,'购买藏品'.$goodsInfo['name'],$price,$userInfo['wx_open_id'],$notify_url);
            if($pay === false) return Response::fail('订单生成失败');
            //生成购买订单
            $order['uid'] = $uid;
            $order['goods_id'] = $goods_id;
            $order['order_num'] = $order_num;
            $order['goods_category_id'] = 1;
            $order['number'] = 1;
            $order['price'] = $price;
            $order['total_price'] = $price;
            $order['pay_type'] = $pay_type;
            $order['create_time'] = $time;
            $result = (new OrdersGoods())->insertGetId($order);
            if($result){
                Db::commit();
                return Response::success('购买成功',['is_pay'=>0,'pay'=>$pay]);
            }
            Db::rollback();
            return Response::fail('购买失败');

        }elseif ($pay_type == 3){
            //微信APP支付
            $notify_url = config('site.wx_app_orders_goods_notify');
            $pay =  (new WxLogic())->appPay($order_num,'购买藏品'.$goodsInfo['name'],$price,$notify_url);
            if($pay === false) return Response::fail('订单生成失败');
            //生成购买订单
            $order['uid'] = $uid;
            $order['goods_id'] = $goods_id;
            $order['order_num'] = $order_num;
            $order['goods_category_id'] = 1;
            $order['number'] = 1;
            $order['price'] = $price;
            $order['total_price'] = $price;
            $order['pay_type'] = $pay_type;
            $order['create_time'] = $time;
            $result = (new OrdersGoods())->insertGetId($order);
            if($result){
                Db::commit();
                return Response::success('购买成功',['is_pay'=>0,'pay'=>$pay]);
            }
            Db::rollback();
            return Response::fail('购买失败');
        }elseif ($pay_type == 4){
            //支付宝APP支付
            $notify_url = config('site.ali_app_orders_goods_notify');
            $pay =  (new AliLogic())->appPay($order_num,'购买藏品'.$goodsInfo['name'],$price,$notify_url);
            if($pay === false) return Response::fail('订单生成失败');
            //生成购买订单
            $order['uid'] = $uid;
            $order['goods_id'] = $goods_id;
            $order['order_num'] = $order_num;
            $order['goods_category_id'] = 1;
            $order['number'] = 1;
            $order['price'] = $price;
            $order['total_price'] = $price;
            $order['pay_type'] = $pay_type;
            $order['create_time'] = $time;
            $result = (new OrdersGoods())->insertGetId($order);
            if($result){
                Db::commit();
                return Response::success('购买成功',['is_pay'=>0,'pay'=>$pay]);
            }
            Db::rollback();
            return Response::fail('购买失败');

        }else{
            return Response::fail('支付方式错误');
        }
    }

    /**
     * 积分兑换
     * @param $uid
     * @param $goodsInfo
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function integralGoods($uid,$goodsInfo){
        $price  = $goodsInfo['price'];
        $goods_id = $goodsInfo['id'];
        $order_num = uniqueNum();
        $time = date('Y-m-d H:i:s');
        Db::startTrans();
        $result = (new AccountLogic())->subAccount($uid,2,$price,'NFT兑换','兑换NFT'.$goodsInfo['name'].'账户扣除');
        if(!$result){
            Db::rollback();
            return Response::fail('积分不足');
        }
        //生成购买订单
        $order['uid'] = $uid;
        $order['goods_id'] = $goods_id;
        $order['order_num'] = $order_num;
        $order['goods_category_id'] = 3;
        $order['number'] = 1;
        $order['price'] = $price;
        $order['total_price'] = $price;
        $order['pay_type'] = 5;
        $order['pay_time'] = $time;
        $order['create_time'] = $time;
        $order['status'] = 1;
        $result = (new OrdersGoods())->insertGetId($order);
        if(!$result){
            Db::rollback();
            return Response::fail('兑换失败');
        }
        $goods['uid'] = $uid;
        $goods['goods_id'] = $goods_id;
        $goods['price'] = $price;
        $goods['create_time'] = date('Y-m-d H:i:s');
        $result =  $this->usersGoodsData->saveEntityAndGetId($goods);
        if($result >0){
            Db::commit();
            $goodsData = new Goods();
            $goodsData->where(['id'=>$goods_id])->setInc('sales');
            $goodsData->where(['id'=>$goods_id])->setDec('surplus');
            return Response::success('兑换成功',['is_pay'=>1]);
        }
        Db::rollback();
        return Response::fail('兑换失败');
    }


    /**
     * 会员NFT作品列表
     * @param $uid
     * @param $status
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function usersGoodsList($uid,$status){
        $where['ug.is_del'] = 0;
        $where['ug.uid'] = $uid;
        if($status > 1){
            $where['ug.status'] = ['in',[2,3,4]];
        } else{
            $where['ug.status'] = ['in',[1,5]];
        }
        $field = ['ug.id','ug.price','ug.is_show','ug.status','g.name','g.image','g.desc','a.name author_name','a.image author_image','n.name network_name'];
        $data = $this->usersGoodsData->alias('ug')
            ->join('goods g','g.id = ug.goods_id')
            ->join('author a','a.id = g.author_id')
            ->join('goods_category gc','gc.id = g.goods_category_id')
            ->join('network n','n.id = g.network_id')
            ->where($where)
            ->field($field)
            ->order(['ug.id desc'])
            ->select();
        if(!empty($data)) {
            $data = collection($data)->toArray();
            $data = addWebSiteUrl($data,['image','author_image']);
            return Response::success('success',$data);
        }
        return Response::success('暂无数据',[]);
    }

    /**
     * 会员作品详情
     * @param $uid
     * @param $id
     * @return array
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function usersGoodsDetail($uid,$id){
        $where['ug.id'] = $id;
        $where['ug.uid'] = $uid;
        $field = ['g.*','ug.price','ug.is_show','ug.status','ug.id','g.id goods_id','a.name author_name','a.image author_image','n.name network_name'];
        $data = $this->usersGoodsData->alias('ug')
            ->join('goods g','g.id = ug.goods_id')
            ->join('author a','a.id = g.author_id')
            ->join('goods_category gc','gc.id = g.goods_category_id')
            ->join('network n','n.id = g.network_id')
            ->where($where)
            ->field($field)
            ->find();
        if($data){
            $data = $data->toArray();
            $data = addWebSiteUrl($data,['image','author_image']);
            return Response::success('success',$data);
        }
        return Response::fail('盲盒信息错误');
    }
    /**
     * 出售藏品
     * @param $uid
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sale($uid,$id,$price){
        if($price < 0.01) return Response::fail('价格输入错误');
        $is_trade = config('site.is_trade');
        if($is_trade == 0) return Response::fail('市场未开启');
        $info = $this->usersGoodsData->where(['uid'=>$uid,'id'=>$id,'status'=>1])->find();
        if(empty($info)) return Response::fail('盲盒信息错误');
        $result = $this->usersGoodsData->where(['id'=>$id])->update(['status'=>2,'price'=>$price,'sales_time'=>date('Y-m-d H:i:s')]);
        if($result) return Response::success('出售成功');
        return  Response::fail('出售失败');
    }

    /**
     * 转赠
     * @param $uid
     * @param $id
     * @param $phone
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function transfer($uid,$id,$phone){
        $targetInfo =  (new Users())->where(['is_del'=>0,'phone'=>$phone])->find();
        if(empty($targetInfo)) return Response::fail('转赠信息错误');
        $info =  $this->usersGoodsData->where(['uid'=>$uid,'id'=>$id,'status'=>['in',[1,2]]])->find();
        if(empty($info)) return Response::fail('作品信息错误');
        //生成转增记录
        $time = date('Y-m-d H:i:s');
        $transfer['users_goods_id'] = $id;
        $transfer['uid'] = $uid;
        $transfer['target_uid'] = $targetInfo['id'];
        $transfer['goods_id'] = $info['goods_id'];
        $transfer['price'] = $info['price'];
        $transfer['create_time'] = $time;
        Db::startTrans();
        $result = (new GoodsTransfer())->insertGetId($transfer);
        if(!$result){
            Db::rollback();
            return Response::fail('转赠失败');
        }
        $result  = $this->usersGoodsData->where(['id'=>$id])->update(['status'=>5]);
        if(!$result){
            Db::rollback();
            return Response::fail('转赠失败');
        }
        //生成新纪录
        $goods['uid'] = $targetInfo['id'];
        $goods['goods_id'] = $info['goods_id'];
        $goods['price'] = $info['price'];
        $goods['goods_num'] = $info['goods_num'];
        $goods['create_time'] = $time;
        $result =  $this->usersGoodsData->insertGetId($goods);
        if($result){
            Db::commit();
            return Response::success('转赠成功');
        }
        Db::rollback();
        return Response::fail('转赠失败');
    }
    /**
     * 修改藏品价格
     * @param $uid
     * @param $id
     * @param $price
     * @return array
     */
    public function updatePrice($uid,$id,$price){
        if($price < 0.01) return Response::fail('价格输入错误');
        $result =  $this->usersGoodsData->where(['uid'=>$uid,'id'=>$id,'status'=>['in',[1,2]]])->update(['price'=>$price]);
        if($result) return Response::success('修改成功');
        return Response::fail('修改失败');
    }

    /**
     * 切换商品状态
     * @param $uid
     * @param $id
     * @param $is_show
     * @return array
     */
    public function updateShow($uid,$id,$is_show){
        $result =  $this->usersGoodsData->where(['uid'=>$uid,'id'=>$id])->update(['is_show'=>$is_show]);
        if($result) return Response::success($is_show == 1?'上架成功':'下架成功');
        return Response::fail($is_show == 1?'上架失败':'下架失败');
    }
}